//
//  ActivityDetailViewController.m
//  豆瓣2
//
//  Created by dllo on 15/11/6.
//  Copyright (c) 2015年 张鸿峰. All rights reserved.
//

#import "ActivityDetailViewController.h"

#import "ActivityDetailView.h"

#import "LoginViewController.h"

#import "DataBaseSingleTon.h"

@interface ActivityDetailViewController ()

@property (nonatomic,retain) ActivityDetailView *activityDetailV;

@end

@implementation ActivityDetailViewController

- (void)dealloc
{
    [_activityDetailV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:0.137 green:1.000 blue:0.700 alpha:1.000];
    
    self.activityDetailV = [[ActivityDetailView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:self.activityDetailV];
    [self.activityDetailV release];
    
    
    self.activityDetailV.activityList = self.activityList;
    self.activityDetailV.activityDetail = self.activityDetail;
    
    
    UIImage *leftBarButtonImage = [UIImage imageNamed:@"btn_nav_back.png"];
    //取消图片渲染
    leftBarButtonImage = [leftBarButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:leftBarButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonItemAction:)] autorelease];
    
    
    UIImage *rightBarButtonImage = [UIImage imageNamed:@"btn_nav_share.png"];
    //取消图片渲染
    rightBarButtonImage = [rightBarButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:rightBarButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonItemAction:)] autorelease];

    
    
    [[DataBaseSingleTon shareDataBase] createActivityStoreTable];
}

- (void)leftButtonItemAction:(UIBarButtonItem *)leftButtonItem
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)UIAlertController:(NSString *)str
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alertC animated:YES completion:^{
        
    }];
   
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}


- (void)rightButtonItemAction:(UIBarButtonItem *)rightButtonItem
{
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName1"];
    
    if (isLogin.length > 0) {
        
        NSLog(@"self.activityList.title = %@",self.activityList.title);
        
        NSMutableArray *arrM = [[DataBaseSingleTon shareDataBase] selectActivityStoreSQL:[NSString stringWithFormat:@"SELECT * FROM activityStore WHERE userName = '%@' AND title = '%@'",isLogin,self.activityList.title]];
        if (arrM.count == 0) {
            
            NSString *sql = [NSString stringWithFormat:@"INSERT INTO activityStore(title, userName) VALUES('%@', '%@')", self.activityList.title, isLogin];
            [[DataBaseSingleTon shareDataBase] insertActivitysql:sql];
            
            [self UIAlertController:@"收藏成功"];
        }
        else
        {
            [self UIAlertController:@"已经收藏"];
        }
    }
    else
    {
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];
        [loginVC release];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
